home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / roids / shot.dist < prev    next >
Encoding:
Text File  |  1995-05-03  |  3.4 KB  |  129 lines

  1. /*
  2.  * Copyright 1989 Digital Equipment Corporation
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted,
  6.  * provided that the above copyright notice appear in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation, and that the name of Digital Equipment
  9.  * Corporation not be used in advertising or publicity pertaining to
  10.  * distribution of the software without specific, written prior
  11.  * permission.  Digital Equipment Corporation makes no representations
  12.  * about the suitability of this software for any purpose.  It is
  13.  * provided "as is" without express or implied warranty.
  14.  *
  15.  * DIGITAL EQUIPMENT CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16.  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR
  18.  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20.  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Terry Weissman
  24.  *          weissman@decwrl.dec.com
  25.  */
  26.  
  27. /* shot.c - handle movement, etc. of the shots. */
  28.  
  29. #include "roids.h"
  30.  
  31. #define MAXSHOTS    20
  32.  
  33. typedef struct _ShotRec {
  34.     double x, y;        /* Location of this shot. */
  35.     double vx, vy;        /* Velocity of this shot. */
  36.     int count;            /* Countdown of life remaining to this shot. */
  37. } ShotRec, *Shot;
  38.  
  39. ShotRec shots[MAXSHOTS];
  40.  
  41. static int numshots;        /* Number of shots currently flying. */
  42.  
  43. static int shotcount;        /* Initial value for life countdown. */
  44.  
  45. static void PaintShot(shot, gc)
  46. Shot shot;
  47. GC gc;
  48. {
  49.     AddLine(rint(shot->x), rint(shot->y),
  50.         rint(shot->x - shot->vx), rint(shot->y - shot->vy), gc);
  51. }
  52.  
  53.  
  54. void PaintAllShots()
  55. {
  56.     int i;
  57.     for (i=0 ; i<numshots ; i++)
  58.     PaintShot(shots + i, shotgc);
  59. }
  60.  
  61.  
  62. static void DestroyShot(i)
  63. int i;
  64. {
  65.     PaintShot(shots + i, backgc);
  66.     numshots--;
  67.     shots[i] = shots[numshots];
  68. }
  69.  
  70.  
  71. void MoveShots(closure, id)
  72. Opaque closure;
  73. XtIntervalId id;
  74. {
  75.     int i;
  76.     double newx, newy;
  77.     Shot shot;
  78.     if (closure != (Opaque) MoveShots) return;
  79.     if (numshots > 0)
  80.         shottimerid = XtAddTimeOut(shotwait, MoveShots, (Opaque) MoveShots);
  81.     else
  82.     shottimerid = NULL;
  83.     for (i=0 ; i<numshots ; i++) {
  84.     shot = shots + i;
  85.     newx = shot->x + shot->vx;
  86.     newy = shot->y + shot->vy;
  87.     if (LineHitsRock(rint(shot->x - shot->vx), rint(shot->y - shot->vy),
  88.              rint(newx), rint(newy))
  89.         || --(shot->count) == 0) {
  90.         DestroyShot(i);
  91.         i--;        /* Ensures we don't skip moving a shot. */
  92.     } else {
  93.         PaintShot(shot, backgc);
  94.         shot->x = newx < 0 ? newx + gamewidth :
  95.         (newx > gamewidth ? newx - gamewidth : newx);
  96.         shot->y = newy < 0 ? newy + gameheight :
  97.         (newy > gameheight ? newy - gameheight : newy);
  98.         PaintShot(shot, shotgc);
  99.     }
  100.     }
  101. }
  102.  
  103.  
  104.  
  105. void AddShot(x, y, vx, vy)
  106. double x, y, vx, vy;
  107. {
  108.     Shot shot;
  109.     if (numshots >= maxshots) return;
  110.     shot = shots + numshots;
  111.     numshots++;
  112.     shot->x = x;
  113.     shot->y = y;
  114.     shot->vx = vx;
  115.     shot->vy = vy;
  116.     shot->count = shotcount;
  117.     if (shottimerid == NULL)
  118.         shottimerid = XtAddTimeOut(shotwait, MoveShots, (Opaque) MoveShots);
  119. }
  120.  
  121.  
  122.  
  123. InitShot()
  124. {
  125.     shottimerid = NULL;
  126.     shotcount = shotduration / shotwait;
  127.     numshots = 0;
  128. }
  129.